import DefaultTheme from 'vitepress/theme'
import './style/var.scss'

import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'
import 'element-plus/theme-chalk/dark/css-vars.css'
// 图标并进行全局注册
import * as ElementPlusIconsVue from '@element-plus/icons-vue'

import VPDemo from '../vitepress'

import { defineAsyncComponent } from 'vue'

const components = import.meta.glob('../vitepress/components/**/*.{vue,ts,tsx}')

export default {
  ...DefaultTheme,
  enhanceApp(ctx) {
    DefaultTheme.enhanceApp(ctx)
    // 注册ElementPlus
    ctx.app.use(ElementPlus)
    // 注册所有图标
    for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
      ctx.app.component(key, component)
    }
    ctx.app.component('Demo', VPDemo)

    // 注册所有组件
    for (const [key, component] of Object.entries(components)) {
      const name = key.match(/\/([^\/]+)\.vue$/)?.[1] ?? ''
      ctx.app.component(name, defineAsyncComponent(component))
    }
  },
}
